package com.example.demo.verify.validator;

import com.example.demo.verify.FileValid;
import com.example.demo.common.Valid;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.List;

public class FileValidValidator implements ConstraintValidator<FileValid, Object> {
    private String size;
    private String[] suffix;

    @Override
    public void initialize(FileValid param) {
        this.size = param.size();
        this.suffix = param.suffix();
    }

    @Override
    public boolean isValid(Object obj, ConstraintValidatorContext context) {
        // 设置校验出错提示
        context.disableDefaultConstraintViolation();

        // 文件对象不能为null
        if (obj == null) {
            context.buildConstraintViolationWithTemplate("文件对象不能为空").addConstraintViolation();
            return false;
        }

        // 文件对象转换
        MultipartFile multipartfile;
        try {
            multipartfile = (MultipartFile)obj;
        }
        catch (Exception ex) {
            context.buildConstraintViolationWithTemplate("文件对象转换异常").addConstraintViolation();
            return false;
        }

        // 校验文件格式
        String fileExname = multipartfile.getOriginalFilename().substring(multipartfile.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
        if (fileExname != null && !fileExname.isEmpty()) {
            List<String> suffixList = Arrays.asList(this.suffix);
            if (!suffixList.contains(fileExname)) {
                context.buildConstraintViolationWithTemplate("文件格式错误").addConstraintViolation();
                return false;
            }
        }
        else {
            context.buildConstraintViolationWithTemplate("文件格式错误").addConstraintViolation();
            return false;
        }

        // 校验文件大小
        if (!Valid.isEmpty(size)) {
            if (!Valid.isNum(size.replace("K", "").replace("M", "").replace("G", ""))) {
                context.buildConstraintViolationWithTemplate("文件大小的校验数字错误").addConstraintViolation();
                return false;
            }

            long ruleSize = 0;
            if (size.contains("K")) {
                size = size.replace("K", "");
                ruleSize = Integer.parseInt(size) * 1024L;
            }
            else if (size.contains("M")) {
                size = size.replace("M", "");
                ruleSize = Integer.parseInt(size) * 1024L * 1024L;
            }
            else if (size.contains("G")) {
                size = size.replace("G", "");
                ruleSize = Integer.parseInt(size) * 1024L * 1024L * 1024L;
            }
            else {
                context.buildConstraintViolationWithTemplate("文件大小的校验单位错误").addConstraintViolation();
                return false;
            }

            long fileSize = multipartfile.getSize();
            if (fileSize > ruleSize) {
                context.buildConstraintViolationWithTemplate("文件大小超出范围").addConstraintViolation();
                return false;
            }
        }

        return true;
    }
}
